ANIMA.add("anima.hundred", function(A) {

A.namespace("anima.hundred");

A.anima.hundred = function(doc, composition) {
   this.doc = doc;
   this.composition = composition;
   this.presentationList = [];
   this.clearList = [];
};

/* static method */
A.anima.hundred.start = function(doc, runFn) {
   var composition = anima.composition.create(
      function(composition) {
         composition.hundred.parseDocument(
               composition.hundred.getDocument(), composition);
      },
      function(composition) {
         composition.hundred.presentComponents();
         if (composition.hundred.runFn != null)
            composition.hundred.runFn(composition);
      }
   );
   
   var hundredInst = new anima.hundred(
         (doc && doc != null) ? doc : document.body,
         composition);
   
   hundredInst.composition.hundred = hundredInst;
   hundredInst.composition.hundred.runFn = (runFn) ? runFn : null;
   
   hundredInst.composition.run();
   
   return composition;
};

/* instance methods */
A.anima.hundred.prototype = {

getDocument: function() {
   return this.doc;
},
      
parseDocument: function() {
   this.parseElement(this.doc);
},

parseElement: function(element) {
   var node = element.firstChild;
   while (node != null) {
      if (node.nodeType === 1) {
         switch (node.className.toLowerCase()) {
            case "dcc" : this.parseComponent(node);
                         break;
            case "connector" : this.parseConnector(node);
                               break;
            default : this.parseElement(node);
         }
      }
      node = node.nextSibling;
   }
},

parseComponent: function(element) {
   var webid = null;
   var instanceid = null;
   var showHere = true;
   
   var node = element.firstChild;
   while (node != null) {
      if (node.nodeType === 1) {
         if (node.className.toLowerCase() === "property") {
            var value = node.firstChild.nodeValue;
            
            switch (node.attributes.getNamedItem("name").value.toLowerCase()) {
               case "type" : webid = "<" + value + ">";
                             break;
               case "id" : instanceid = value;
                           break;
               case "showhere": if (value === "false") showHere = false;
                                break;
            }
         }
      }
      node = node.nextSibling;
   }
   
   this.composition.createInstance(webid, instanceid);
   this.clearList.push(element);
   if (showHere)
      this.presentationList[instanceid] = element;
},

parseConnector: function(element) {
   var instanceidRequires = null;
   var instanceidProvides = null;
   
   var node = element.firstChild;
   while (node != null && (instanceidProvides == null)) {
      if (node.nodeType === 1) {
         if (node.className.toLowerCase() === "component") {
            var value = node.firstChild.nodeValue;
            if (instanceidRequires == null)
               instanceidRequires = value;
            else
               instanceidProvides = value;
         }
      }
      node = node.nextSibling;
   }
   
   this.composition.connect(instanceidRequires, instanceidProvides);
   this.clearList.push(element);
},

presentComponents: function() {
   // clear all hundred divs
   for (c in this.clearList)
      this.clearList[c].innerHTML = "";
   
   // present "presentable" components
   for (c in this.presentationList) {
      var component = this.composition.component(c).queryInterface(
            "<http://purl.org/dcc/view.IWComponent>");
      if (component != null)
         this.presentationList[c].appendChild(component.getPresentation());
   }
}

};

});